<template>
  <div class="mod-menu">
    <!-- <el-form :inline="true" :model="pamars">
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">添加</el-button>
      </el-form-item>
    </el-form> -->
    <el-table :data="dataList" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="title" align="center" label="模板消息名称">
      </el-table-column>
      <el-table-column prop="wx_tempid" align="center" label="模板消息ID">
      </el-table-column>
      <el-table-column prop="is_use" align="center" label="开启">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.is_use" :active-value="true" :inactive-value="false" 
            inactive-color="#B9B9B9" @change="changeSwitch(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column  header-align="center" align="center"  label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChange" @current-change="currentChange" :page-size="pamars.limit" :page-sizes="[10, 20, 50, 100]"
      :current-page="pamars.page" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <news-add v-if="addOrUpdateVisible" ref="addOrUpdate" @updataList='updataList'></news-add>
  </div>
</template>

<script>
  import seviceApi from '@/api/serviceApi.js'
  import newsAdd from './news-add.vue'
  export default {
    components: {
      newsAdd
    },
    data () {
      return {
        pamars: {
          page: 1,
          limit: 10
        },
        totalPage: 0,
        dataList: [],
        addOrUpdateVisible: false
      }
    },
    mounted () {
      this.getlist()
    },
    methods: {
      // 更新列表
      updataList (e) {
        if (e == 1) {
          this.getlist()
        }
      },
      // 每页数
      sizeChange (val) {
        this.pamars.limit = val
        this.getList()
      },
      // 当前页
      currentChange (val) {
        this.pamars.page = val
        this.getList()
      },
      // 获取列表数据
      getlist () {
        seviceApi.getNotifytempList(this.pamars).then(res => {
          if (res && res.data.code == 200) {
            this.dataList = res.data.page.list
            this.totalPage = res.data.page.totalCount
          }
        })
      },
      // 启用
      changeSwitch (row) {
        let pamars = {
          id: row.id,
          is_use: row.is_use == true ? 'true' : 'false'
        }
        seviceApi.useNotifytemp(pamars).then(res => {
          if (res && res.data.code == 200) {
            this.getlist()
          }
        })
      },
      // 添加
      addOrUpdateHandle () {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init()
        })
      },
      // 修改
      edit (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.info(row)
        })
      }
    }
  }
</script>

<style>
</style>
